import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:scrollview_observer/scrollview_observer.dart';
import 'package:ball_lion/module/team/basketball/lq_team_detail_logic.dart';
import 'package:ball_lion/entity/team/lq_team_schedule_entity.dart';
import 'package:ball_lion/server/api/basketball_api.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';

class LqTeamScheduleLogic extends GetxController {
  LqTeamDetailLogic detail = Get.find(tag: "${Get.arguments}");
  int _yearIndex = 0;
  int _typeIndex = 0;
  List<LeagueArray>? typeList;
  List<LqTeamScheduleYearEntity>? _years;
  LqTeamScheduleEntity? _data;
  ScrollController scrollC = ApmScrollController();
  late ListObserverController observerController;
  List<MatchGroup>? _matchGroup = [];
  bool _toNull = false;

  bool get toNull => _toNull;
  int get typeIndex => _typeIndex;
  int get yearIndex => _yearIndex;
  LqTeamScheduleEntity? get data => _data;
  List<MatchGroup>? get matchGroup => _matchGroup;
  List<LqTeamScheduleYearEntity>? get years => _years;

  set toNull(bool value) {
    _toNull = value;
    update();
  }

  set typeIndex(int value) {
    _typeIndex = value;
    update();
  }

  Future showDatePicker() async {
    _yearIndex = await Get.bottomSheet(BottomPickerWidget(
      _years?.map((e) => e.year!.toString()).toList() ?? [],
      title: '选择年份',
      initialIndex: _yearIndex,
    ));
    update();
  }

  void getType(index) {
    _typeIndex = index;
    if (_typeIndex != 0) {
      LqTeamScheduleEntity d =
          LqTeamScheduleEntity.fromJson(_data?.toJson() ?? {});
      if (detail.data?.national == 1 ||
          (_data?.leagueArray?[_typeIndex].leagueId ?? 0) > 10) {
        d.matchGroup?.removeWhere((element) =>
            element.matchArray?.every((e) =>
                e.leagueId != _data?.leagueArray?[_typeIndex].leagueId) ??
            true);
        d.matchGroup?.forEach((element) {
          element.matchArray?.removeWhere(
              (e) => e.leagueId != _data?.leagueArray?[_typeIndex].leagueId);
        });
      } else {
        d.matchGroup?.removeWhere((element) =>
            element.matchArray?.every(
                (e) => e.kind != _data?.leagueArray?[_typeIndex].leagueId) ??
            true);
        d.matchGroup?.forEach((element) {
          element.matchArray?.removeWhere(
              (e) => e.kind != _data?.leagueArray?[_typeIndex].leagueId);
        });
      }
      _matchGroup = List.from(d.matchGroup ?? []);
    } else {
      _matchGroup = List.from(_data?.matchGroup ?? []);
    }
    update();
  }

  Future requestYears() async {
    _years = await BasketballApi.getLqTeamScheduleYear(detail.teamId);
    update();
  }

  Future requestData() async {
    if (_years.hasValue) {
      if (detail.data?.national == 1) {
        _data = await BasketballApi.getLqTeamSchedule(
            detail.teamId, _years?[_yearIndex].year.toString() ?? "",
            leagueId: typeList != null ? typeList![_typeIndex].leagueId : null);
      } else {
        _data = await BasketballApi.getLqTeamSchedule(
            detail.teamId, _years?[_yearIndex].year.toString() ?? "",
            kind: typeList != null ? typeList![_typeIndex].leagueId : null);
      }
      typeList = _data?.leagueArray;
      _matchGroup = List.from(_data?.matchGroup ?? []);
      update();
    }
  }

  void initObserver() {
    double offset = 0;
    var length = 0;
    int index = _data?.matchGroup?.indexWhere((element) =>
            element.title == DateTime.now().formatedString('yyyy-MM')) ??
        0;
    if (index != 0) {
      for (var i = 0; i < index; i++) {
        length = length + (_data?.matchGroup?[i].matchArray?.length ?? 0);
      }
      offset = 56.0 * index + 50 * length;
    }
    scrollC.jumpTo(offset);
  }

  String formScore(int type, MatchArray? entity) {
    switch (entity?.statusId) {
      case 10:
        if (entity?.guestPoints != null && entity?.homePoints != null) {
          return "${entity!.guestPoints!}-${entity.homePoints!}";
        } else {
          return "vs";
        }
      case 12:
        return "取消";
      case 11:
        return "中断";
      case 14:
        return "腰斩";
      case 13:
        return "延期";
      case 15:
        return "待定";
      default:
        return "vs";
    }
  }
}
